package com.milhouz.quizzdroid.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class QuizzDroidUserOpenHelper extends SQLiteOpenHelper {

	public QuizzDroidUserOpenHelper(Context context) {
		super(context, QuizzDroidConstants.USER_DATABASE_NAME, null, QuizzDroidConstants.USER_DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + QuizzDroidConstants.TABLE_NAME_USERS + " (" //
				+ QuizzDroidConstants.ID + " INTEGER PRIMARY KEY," //
				+ QuizzDroidConstants.COLUMN_NAME_USERNAME + " TEXT," //
				+ QuizzDroidConstants.COLUMN_NAME_TOTAL_QUESTIONS + " INTEGER," //
				+ QuizzDroidConstants.COLUMN_NAME_NB_GOOD_RESPONSES + " INTEGER," //
				+ QuizzDroidConstants.COLUMN_NAME_MEANTIME + " FLOAT" + ");");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// Logs that the database is being upgraded
		Log.w(QuizzDroidConstants.TAG, "Upgrading user database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
		// Kills the table and existing data
		db.execSQL("DROP TABLE IF EXISTS " + QuizzDroidConstants.TABLE_NAME_USERS);
		// Recreates the database with a new version
		onCreate(db);
	}
}
